﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class DicType: IDicType
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public DicType(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.DicType GetDicType(System.Int32 dicTypeID)
		{
			BEGDC.PreContract.DAL.DicType dicType = BEGDC.PreContract.DAL.DicType.TryFind(dicTypeID);

            return dicType;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetDicType_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM DicType";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteDicType(System.Int32 dicTypeID)
		{
			try
            {
				BEGDC.PreContract.DAL.DicType dicType = BEGDC.PreContract.DAL.DicType.TryFind(dicTypeID);
                dicType.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("DicType", string.Format("Error occured when deleting the DicType, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
		public int AddDicType(System.String dicTypeValue, System.String dicTypeDesc, System.Boolean isSystem, System.Boolean isEnabled)	
		{
			try
            {
				BEGDC.PreContract.DAL.DicType dicType = new BEGDC.PreContract.DAL.DicType();
				
				dicType.DicTypeValue = dicTypeValue;
				dicType.DicTypeDesc = dicTypeDesc;
				dicType.IsSystem = isSystem;
				dicType.IsEnabled = isEnabled;
				dicType.CreateAndFlush();
				
				return dicType.DicTypeID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("DicType", string.Format("Error occured when adding a new DicType, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
		public bool UpdateDicType(System.Int32 dicTypeID, System.String dicTypeValue, System.String dicTypeDesc, System.Boolean isSystem, System.Boolean isEnabled)
		{
			try
            {
                BEGDC.PreContract.DAL.DicType dicType = GetDicType(dicTypeID);

				dicType.DicTypeValue = dicTypeValue;
				dicType.DicTypeDesc = dicTypeDesc;
				dicType.IsSystem = isSystem;
				dicType.IsEnabled = isEnabled;
				dicType.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("DicType", string.Format("Error occured when updating the DicType, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



